package com.tju.resource.oauth.controller.tb;

import com.tju.resource.model.TbSharper;
import com.tju.resource.server.TbSharperService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.tool.ClientParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 磨具
 */
@RestController
public class TbSharperController {
    private static final String prefix="sharper";
    @Autowired
    private TbSharperService tbSharperService;
    private Lock lock =new ReentrantLock(true); //安全锁
    /**
     * 查询磨具信息
     */
    @PostMapping(prefix + "/getSharper")
    @PreAuthorize("hasAuthority('sys:tool:sharper:list')")
    public Result<Object> list(@RequestBody ClientParameters parameter) {
        return tbSharperService.selectTbSharper(parameter);
    }

    /**
     * 添加磨具信息
     */
    @PostMapping(prefix + "/addSharper")
    @PreAuthorize("hasAuthority('sys:tool:sharper:add')")
    public Result<Object> add(@RequestBody TbSharper TbSharper) {
        try {
            lock.lock();
            return tbSharperService.insertTbSharper(TbSharper);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }finally{
            lock.unlock();
        }
    }

    /**
     * 修改磨具信息
     */
    @PostMapping(prefix + "/updateSharper")
    @PreAuthorize("hasAuthority('sys:tool:sharper:edit')")
    public Result<Object> edit(@RequestBody TbSharper TbSharper) {
        try {
            lock.lock();
            return tbSharperService.updateTbSharper(TbSharper);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }finally{
            lock.unlock();
        }

    }

    /**
     * 删除磨具
     */
    @PostMapping(prefix + "/deleteSharper")
    @PreAuthorize("hasAuthority('sys:tool:sharper:delete')")
    public Result<Object> delete(@RequestBody TbSharper TbSharper)
    {
        try {
            lock.lock();
            return tbSharperService.deleteTbSharper(TbSharper);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }finally{
            lock.unlock();
        }

    }
}
